<template>
    <div class="app-container">
        <div class="header">
            <el-button type="primary" size="mini" @click="showSaveCategoryForm = true">添加</el-button>
            <el-input style="width: 300px; margin-left: 100px;" v-model="categoryListForm.memo" size="mini"
                placeholder="输入关键字搜索" @change="categorySearch" @input="checkVal">
                <i slot="suffix" class="el-icon-search"></i>
            </el-input>
        </div>

        <!-- 添加课程类别弹出框 -->
        <el-dialog title="添加课程类别" :visible.sync="showSaveCategoryForm" :close-on-click-modal="false"
            @close="closeCategoryForm">
            <el-form ref="saveCategoryForm" :model="saveCategoryForm" label-width="110px" :rules="saveRules">
                <el-form-item label="课程类别名称" prop="name">
                    <el-input v-model="saveCategoryForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="saveBrands" :loading="loading">提交</el-button>
                    <el-button @click="closeCategoryForm">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <!-- 课程类别信息列表 -->
        <div class="content">
            <el-table :data="categoryList" style="width: 100%;" stripe
                :default-sort="{ prop: 'date', order: 'descending' }">
                <el-table-column label="课程类别信息">
                    <el-table-column label="课程类别名称" prop="name">
                        <template slot-scope="scope">
                            <div>{{ scope.row.categoryName }}</div>
                        </template>
                    </el-table-column>
                </el-table-column>
                <el-table-column label="操作">
                    <el-table-column label="删除">
                        <template slot-scope="scope">
                            <el-button type="danger" circle icon="el-icon-delete"
                                @click="delCategory(scope.row.categoryId)"></el-button>
                        </template>
                    </el-table-column>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="categoryListSizeChange" @current-change="categoryListCurrentChange"
                :page-size="categoryListForm.size" :page-sizes="[10, 20, 30]" layout="total, sizes, prev, pager, next"
                :total="categoryListTotal" style="margin-top: 20px;">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { Message } from 'element-ui'
import utils from '@/utils/checkLogin'
import { getCategoryList, saveCategory, deleteCategory } from '../../api/api'
export default {
    data() {
        return {
            loading: false,
            showSaveCategoryForm: false,
            categoryListSearch: '',
            categoryListForm: {
                page: 1,
                size: 10,
                memo: ''
            },
            categoryList: [],
            categoryListTotal: 0,
            saveRules: {
                name: [
                    { required: true, message: '请输入课程类别名称', trigger: 'change' }
                ]
            },
            showSaveCategoryForm: false,
            saveCategoryForm: {
                name: ''
            },
        }
    },
    methods: {
        // 课程类别列表
        getCategoryListData() {
            getCategoryList(this.categoryListForm).then((res) => {
                if (res.code == 200) {
                    console.log('商品课程类别列表', res)
                    this.categoryList = res.data
                    this.categoryListTotal = res.total
                }
            })
        },
        // 添加-保存-提交
        saveBrands() {
            this.$refs.saveCategoryForm.validate(valid => {
                if (!valid) {
                    Message({
                        message: '请检查表单',
                        type: 'warning',
                        duration: 2000,
                    })
                    return false
                } else {
                    saveCategory(this.saveCategoryForm).then((res) => {
                        if (res.code == 200) {
                            Message({
                                message: '添加成功',
                                type: 'success',
                                duration: 3000
                            })
                            console.log('添加课程类别', res)
                            this.loading = true
                            this.getCategoryListData()
                            setTimeout(() => {
                                this.showSaveCategoryForm = false
                                this.loading = false
                                this.$nextTick(() => {
                                    this.$refs.saveCategoryForm.resetFields()
                                })
                            }, 1500)
                        } else {
                            Message(({
                                message: '添加失败',
                                type: 'success',
                                duration: 2000
                            }))
                        }
                    })
                }
            })

        },
        delCategory(id) {
            this.$confirm('是否删除该课程类别?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCategory(id).then((res) => {
                    if (res.code == 200) {
                        console.log('删除成功', res);
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            duration: 3000
                        })
                        this.getCategoryListData()
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败',
                            duration: 3000
                        })
                    }
                })
            }).catch(() => {
            })
        },
        // 当每页条数改变时
        categoryListSizeChange(e) {
            this.categoryListForm.size = e
            this.getCategoryListData()
        },
        // 切换页码时
        categoryListCurrentChange(e) {
            this.categoryListForm.page = e
            this.getCategoryListData()
        },
        // 搜索
        categorySearch() {
            this.getCategoryListData()
        },

        // 当搜索框的值为空时，显示全部课程类别
        checkVal() {
            if (this.categoryListForm.memo == '') {
                this.getCategoryListData()
            }
        },
        closeCategoryForm() {
            this.showSaveCategoryForm = false
            this.$nextTick(() => {
                // 关掉 dialog 时，置空 form 表单
                this.$refs.saveCategoryForm.resetFields()
            })
        },
    },
    created() {
        utils.checkLogin()
        this.getCategoryListData()
    },
}
</script>

<style lang="less" scoped>
.app-container {
    padding: 40px;

    .header {
        align-items: center;
        display: flex;
        margin-bottom: 20px;

        .el-icon-search {
            cursor: pointer;
            margin-right: 4px;
            margin-top: 8px;
        }

    }

    .content {
        text-align: center;
    }
}
</style>